package cn.diancan.service.impl;

import java.io.File; 
import java.util.Random;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Component;

import cn.diancan.config.Config;
import cn.diancan.dao.MyBaseDao;
import cn.diancan.exception.OutMsgException;
import cn.diancan.model.ImageFileA;
import cn.diancan.service.ImageFileAService;
import cn.diancan.util.CompressImg;
import cn.diancan.util.CompressThreadPool;
import cn.diancan.util.DateUtil;
import cn.diancan.util.FileHandle;
import cn.diancan.util.MD5;

@Component("imageFileAService")
public class ImageFileAServiceImpl  implements ImageFileAService {
    
    @Resource(name="MyBaseDao")
    private MyBaseDao<ImageFileA> imageFileADao ;

	@Override
	public String saveImage(File file,String fileName,String fileType,Integer isCompre)throws OutMsgException , Exception { 
		ImageFileA imageFile = new ImageFileA();
		imageFile.setDirCode(Config.dir.getDircode());
		long fileLength=file.length();
		imageFile.setFileLength(fileLength);
		imageFile.setFileType(fileType);
		imageFile.setIsDelete(0);
		imageFile.setSrcFileName(fileName);  
		imageFile.setUploadTime(DateUtil.currentDateTime());
		imageFileADao.save(imageFile);
		if(null == imageFile.getId()){
			throw new OutMsgException("保存失败");
		}
		//现在的文件名 
		fileName=imageFile.getId()+"-"+fileLength+"_"+fileType;
		//imagefilea 表名
		String filePath=Config.dir.getRootpath()+"/ImageFileA"+FileHandle.parsePath(imageFile.getId())+File.separator+fileName; 
		File savefile = new File(filePath);
		if (!savefile.getParentFile().exists()){
			savefile.getParentFile().mkdirs();
		} 
		//file.renameTo(savefile);
		FileUtils.copyFile(file, savefile);
		//放入线程池中压缩
		if(null== isCompre || 1==isCompre){
			CompressThreadPool.pool.execute(new CompressImg(filePath, filePath, 0, 0, false));
		}
		file.delete();
		//裁切图片	
		//				 主机		 方法名	    磁盘号                           文件名
		return Config.dir.getAccesshost()+"img/"+Config.dir.getDircode()+"/YMXXXX"+imageFile.getId()+"-"+fileLength+"_YM0000"+"."+fileType;
	}
	
	@Override
	public String saveSound(File file, String fileName, String fileType)throws OutMsgException, Exception {
	    	ImageFileA imageFile = new ImageFileA();
		imageFile.setDirCode(Config.dir.getDircode());
		long fileLength=file.length();
		imageFile.setFileLength(fileLength);
		imageFile.setFileType(fileType);
		imageFile.setIsDelete(0);
		imageFile.setSrcFileName(fileName);  
		imageFile.setUploadTime(DateUtil.currentDateTime());
		
		String signCode = MD5.createPassword(imageFile.getUploadTime()+fileLength+ new Random().nextInt());
		imageFile.setSignCode(signCode);
		
		imageFileADao.save(imageFile);
		if(null == imageFile.getId()){
			throw new OutMsgException("保存失败");
		}
		//现在的文件名 
		fileName=imageFile.getId()+"-"+signCode+"_"+fileType;
		//imagefilea 表名
		String filePath=Config.dir.getRootpath()+"/ressd"+FileHandle.parsePath(imageFile.getId())+File.separator+fileName; 
		File savefile = new File(filePath);
		if (!savefile.getParentFile().exists()){
			savefile.getParentFile().mkdirs();
		} 
		//file.renameTo(savefile);
		FileUtils.copyFile(file, savefile);
		//放入线程池中压缩 
		file.delete();
		
		return Config.dir.getAccesshost()+"ressd/"+Config.dir.getDircode()+"/"+imageFile.getId()+"-"+signCode+"."+fileType;
	}
	@Override
	public String saveVideo(File file, String fileName, String fileType)throws OutMsgException, Exception {
	    	ImageFileA imageFile = new ImageFileA();
		imageFile.setDirCode(Config.dir.getDircode());
		long fileLength=file.length();
		imageFile.setFileLength(fileLength);
		imageFile.setFileType(fileType);
		imageFile.setIsDelete(0);
		imageFile.setSrcFileName(fileName);  
		imageFile.setUploadTime(DateUtil.currentDateTime());
		
		String signCode = MD5.createPassword(imageFile.getUploadTime()+fileLength+ new Random().nextInt());
		imageFile.setSignCode(signCode);
		
		imageFileADao.save(imageFile);
		if(null == imageFile.getId()){
			throw new OutMsgException("保存失败");
		}
		//现在的文件名 
		fileName=imageFile.getId()+"-"+signCode+"_"+fileType; 
		
		//imagefilea 表名
		String filePath=Config.dir.getRootpath()+"/resvd"+FileHandle.parsePath(imageFile.getId())+File.separator+fileName; 
		File savefile = new File(filePath);
		if (!savefile.getParentFile().exists()){
			savefile.getParentFile().mkdirs();
		} 
		//file.renameTo(savefile);
		FileUtils.copyFile(file, savefile);
		//放入线程池中压缩 
		file.delete();
		
		return Config.dir.getAccesshost()+"resvd/"+Config.dir.getDircode()+"/"+imageFile.getId()+"-"+signCode+"."+fileType;
	}

}
